{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# This notebook has been tested with \n",
    "* Python 3.5\n",
    "* Keras 2.0.6\n",
    "* Tensorflow 1.2.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Use image generator to have more training data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img\n",
    "import concurrent.futures\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Keras documentation for detail\n",
    "# https://keras.io/preprocessing/image/#imagedatagenerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# The variable 'number' gives the numbers of images generated from one image\n",
    "def datagen(filename, destination, number):\n",
    "    datagen = ImageDataGenerator(\n",
    "            rotation_range=0.2,\n",
    "            width_shift_range=0.2,\n",
    "            height_shift_range=0.2,\n",
    "            rescale=1./255,\n",
    "            shear_range=0.2,\n",
    "            zoom_range=0.2,\n",
    "            horizontal_flip=True,\n",
    "            fill_mode='nearest')\n",
    "    \n",
    "    img = load_img(filename)\n",
    "    x = img_to_array(img) \n",
    "    x = x.reshape((1,) + x.shape)\n",
    "    filename = filename.split('/')[1]\n",
    "    \n",
    "    i = 0 \n",
    "    for batch in datagen.flow(x, batch_size=1,\n",
    "                          save_to_dir=destination, save_prefix=filename, save_format='jpg'):\n",
    "        i += 1\n",
    "        if i > number:\n",
    "            break  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# The source directory of original images\n",
    "photoDir = 'middle_hair_man_train/'\n",
    "photoList = os.listdir(photoDir)\n",
    "# The target directory to save images generated\n",
    "direction = 'middle_hair_man_train/'\n",
    "\n",
    "# Use multi-threading to speed up\n",
    "with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:\n",
    "    for photo in photoList:\n",
    "        try:\n",
    "            filename = photoDir+photo\n",
    "            executor.submit(datagen, filename, direction, 20)\n",
    "        except Exception as exc:\n",
    "            print(exc)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
